package engine.ui
{
	import engine.GameData;
	import engine.data.ModelLocator;
	import engine.display.Story.StoryMoveUI;
	import engine.flag.GameDefine;
	import engine.newUi.ActionEctypeResultLogic;
	import engine.newUi.ActionEctypeResultUI;
	import engine.newUi.ActivitesGroupLogic;
	import engine.newUi.BottomIndexUI;
	import engine.newUi.CampBattleUI;
	import engine.newUi.EliteFBListLogic;
	import engine.newUi.EliteFBLogic;
	import engine.newUi.EliteFBUI;
	import engine.newUi.EscortSceneUI;
	import engine.newUi.FactionAttackUI;
	import engine.newUi.FactionBossExitUI;
	import engine.newUi.FactionBossRankUI;
	import engine.newUi.FactionBossUI;
	import engine.newUi.FastTrackUI;
	import engine.newUi.ForbiddenAreaUI;
	import engine.newUi.NextActivityGuideUI;
	import engine.newUi.PkListUI;
	import engine.newUi.RightNoticeLogic;
	import engine.newUi.RightTopIndexUI;
	import engine.newUi.SitCrossleggedLogic;
	import engine.newUi.SnowBossBloodUI;
	import engine.newUi.SnowBossExitUI;
	import engine.newUi.SnowBossRankUI;
	import engine.newUi.StrideMainLogic;
	import engine.newUi.TaskTrackingUI;
	import engine.newUi.TopPlayerLogic;
	import engine.newUi.TopPlayerUI;
	import engine.newUi.TourLogic;
	import engine.newUi.TourUI;
	import engine.newUi.WorldBossBloodUI;
	import engine.newUi.WorldBossExitUI;
	import engine.newUi.WorldBossRankUI;
	import engine.newUi.WorldMapLogic;
	import engine.scene.SceneChangeManager;
	import engine.scene.SceneFrameLogic;
	import engine.scene.SceneManager;
	import engine.scene.ScenePlayerLogic;
	import engine.ui.chat.ChatUI;
	import engine.ui.lite.BaseUI;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Point;
	
	import newx.StageInfo;
	import newx.com.ResManager;
	
	/**
	 *	主界面ui管理 
	 * @author test
	 * 
	 */
	public class MainUIManager
	{
		private static var _self		:MainUIManager;
		
		public static function getInstance():MainUIManager
		{
			if(_self == null)
			{
				_self = new MainUIManager();
			}
			return _self;
		}
		private 	var _model			:ModelLocator;
		
		//ui容器
		private	var _uiPlace		:Sprite;
		//界面容器
		public	 	var downPlace		:Sprite;
		//上部容器(会改变坐标的)
		public	 	var topPlace		:Sprite;
		public		var middlePlace		:Sprite;
		
		//顶部玩家头像（战斗共用）
		private 	var _topPlayerUI	:TopPlayerUI;
		//顶部右边ui
		private	var _rightTopIndexUI:RightTopIndexUI;
		//聊天界面
		private 	var _chatUI			:ChatUI;
		//底部导航
		private	var _bottomIndexUI	:BottomIndexUI;
		//任务追踪
		private	var _taskTrackingUI	:TaskTrackingUI;
		//阵营战界面
		private 	var _campBattleUI	:CampBattleUI;
		//帮派战界面
		private 	var _factionBattleUI:FactionAttackUI;
		
		private	var _nextActivityUI:NextActivityGuideUI;
		
		private 	var _autoFoundWayIcon	:MovieClip;
		
		private	var _oldSceneType:int		=	-1;
		
		private var _autoFoundWay:Boolean	=	false;
		
		private var _bossrank				:WorldBossRankUI;
		
		private var _scenePlayerVisible:Boolean;
		public function MainUIManager()
		{
			init();
		}
		
		private function init():void
		{
			_uiPlace		=	StageInfo.uiSpace;
			
			topPlace		=	new Sprite();
			middlePlace		=	new Sprite();
			downPlace		=	new Sprite();
			_uiPlace.addChild(middlePlace);
			_uiPlace.addChild(topPlace);
			_uiPlace.addChild(downPlace);
			
			_uiPlace.mouseEnabled = downPlace.mouseEnabled = false;
			
			_model			=	ModelLocator.getInstance();
			
			_topPlayerUI	=	TopPlayerUI.getInstance();
			_rightTopIndexUI=	RightTopIndexUI.getInstance();
			_chatUI			=	ChatUI.getInstance();
			_bottomIndexUI	=	BottomIndexUI.getInstance();
			_taskTrackingUI	=	TaskTrackingUI.getInstance();
			_campBattleUI	= 	CampBattleUI.getInstance();
			_bossrank		=  WorldBossRankUI.getInstance();
			_factionBattleUI=	FactionAttackUI.getInstance();
			_nextActivityUI	=	NextActivityGuideUI.getInstance();
			_autoFoundWayIcon	=	ResManager.newResClass("custSkin_100") as MovieClip;
			_autoFoundWayIcon.mouseChildren=_autoFoundWayIcon.mouseEnabled=false;
			_autoFoundWayIcon.stop();
			StageInfo.systemSpace.addChild(_autoFoundWayIcon);
			autoFoundWay	=	false;
		}
		
		/**
		 *	维护ui坐标 
		 */		
		public function onStageReSize():void
		{
			topPlace.y = SceneChangeManager.getGameY();
			_chatUI.y = _chatUI.initPosY;
			
			//自动寻路
			_autoFoundWayIcon.x	=	SceneChangeManager.attackSceneViewX + 500;
			_autoFoundWayIcon.y	=	SceneChangeManager.attackSceneViewY + 250;
			
			//剧情
			StoryMoveUI.getInstance().changeLocation()
			
			//任务追踪
			_taskTrackingUI.changeLocation();
			_nextActivityUI.y	=	52;
			
			var w:int;
			var h:int;
			var point : Point
			
			switch( _oldSceneType )
			{
				case GameDefine.SceneType_Attack:
				{//战斗场景
					_topPlayerUI.x 	=  _chatUI.x	=	SceneChangeManager.attackSceneViewX;
					break;
				}
				case GameDefine.SceneType_Clone://天梯
				case GameDefine.SceneType_Tour://游历
				case GameDefine.SceneType_Map://游历
				{//全屏界面
					_topPlayerUI.x 	=  _chatUI.x	=	SceneChangeManager.attackSceneViewX;
					_chatUI.y = _chatUI.y - 60;
					break;
				}
				case GameDefine.SceneType_WorldBossScene:
				{//世界boos
					WorldBossExitUI.getInstance().x = - SceneChangeManager.getGameMinWidhtOffsize() + 200;
					_bossrank.x = - SceneChangeManager.getGameMinWidhtOffsize() + 200;
					_topPlayerUI.x  = SceneChangeManager.getGameMinWidhtOffsize();
					_chatUI.x		=	_topPlayerUI.x;
					_rightTopIndexUI.x = - (SceneChangeManager.getGameMinWidhtOffsize()-200);
					_nextActivityUI.x	=	_rightTopIndexUI.x;
					
					if( WorldBossBloodUI.getInstance().windowBG != null )
					{
						w = WorldBossBloodUI.getInstance().windowBG.width;
						h = WorldBossBloodUI.getInstance().windowBG.height;
					}else
					{
						w = WorldBossBloodUI.getInstance().width;
						h = WorldBossBloodUI.getInstance().height;
					}
					point = WorldBossBloodUI.getInstance().parent.globalToLocal( new Point( ( StageInfo.stage.stageWidth - w ) * .5,
						( StageInfo.stage.stageHeight - h ) * .2 ) );
					WorldBossBloodUI.getInstance().x = point.x;
					WorldBossBloodUI.getInstance().y = point.y - 20;
					
					break;
				}
				case GameDefine.SceneType_SnowBallFightScene:
				{//雪人boos
					SnowBossExitUI.getInstance().x = - SceneChangeManager.getGameMinWidhtOffsize() + 200;
					SnowBossRankUI.getInstance().x = - SceneChangeManager.getGameMinWidhtOffsize() + 200;
					_topPlayerUI.x  = SceneChangeManager.getGameMinWidhtOffsize();
					_chatUI.x		=	_topPlayerUI.x;
					_rightTopIndexUI.x = - (SceneChangeManager.getGameMinWidhtOffsize()-200);
					_nextActivityUI.x	=	_rightTopIndexUI.x;
					
					if( SnowBossBloodUI.getInstance().windowBG != null )
					{
						w = SnowBossBloodUI.getInstance().windowBG.width;
						h = SnowBossBloodUI.getInstance().windowBG.height;
					}else
					{
						w = SnowBossBloodUI.getInstance().width;
						h = SnowBossBloodUI.getInstance().height;
					}
					point = SnowBossBloodUI.getInstance().parent.globalToLocal( new Point( ( StageInfo.stage.stageWidth - w ) * .5,
						( StageInfo.stage.stageHeight - h ) * .2 ) );
					SnowBossBloodUI.getInstance().x = point.x;
					SnowBossBloodUI.getInstance().y = point.y - 50;
					
					break;
				}
				case GameDefine.SceneType_FactionBossScene:
				{//帮派boos
					FactionBossExitUI.getInstance().x = - SceneChangeManager.getGameMinWidhtOffsize() + 200;
					FactionBossRankUI.getInstance().x = - SceneChangeManager.getGameMinWidhtOffsize() + 200;
					_topPlayerUI.x  = SceneChangeManager.getGameMinWidhtOffsize();
					_chatUI.x		=	_topPlayerUI.x;
					_rightTopIndexUI.x = - (SceneChangeManager.getGameMinWidhtOffsize()-200)
						_nextActivityUI.x	=	_rightTopIndexUI.x;
					
					if( FactionBossUI.getInstance().windowBG != null )
					{
						w = FactionBossUI.getInstance().windowBG.width;
						h = FactionBossUI.getInstance().windowBG.height;
					}else
					{
						w = FactionBossUI.getInstance().width;
						h = FactionBossUI.getInstance().height;
					}
					point = FactionBossUI.getInstance().parent.globalToLocal( new Point( ( StageInfo.stage.stageWidth - w ) * .5,
						( StageInfo.stage.stageHeight - h ) * .2 ) );
					FactionBossUI.getInstance().x = point.x;
					FactionBossUI.getInstance().y = point.y - 20;
					break;
				}
				case GameDefine.SceneType_ForbiddenAreaScene:
				{
					_rightTopIndexUI.x = - (SceneChangeManager.getGameMinWidhtOffsize()-200 + 35);
					ForbiddenAreaUI.getInstance().x 	=  _chatUI.x	=	SceneChangeManager.attackSceneViewX ;
					_bottomIndexUI.x	=	- (SceneChangeManager.getGameMinWidhtOffsize()-200);
					break;
				}
				case GameDefine.SceneType_EscortScene://押镖
				{
					_topPlayerUI.x 	=  _chatUI.x	=	SceneChangeManager.attackSceneViewX;
					w = EscortSceneUI.getInstance().width;
					h = EscortSceneUI.getInstance().height;
					var bgx:Number = EscortSceneUI.getInstance().x;
					var bgy:Number = EscortSceneUI.getInstance().y;
					var bgglobalPoint:Point = EscortSceneUI.getInstance().parent.localToGlobal(new Point(bgx,bgy));
					var uiGlobalpoint:Point = new Point(bgglobalPoint.x ,bgglobalPoint.y + 757 - 161);
					
					var newpoit:Point = ChatUI.getInstance().parent.globalToLocal(uiGlobalpoint);
					
					_chatUI.y = newpoit.y - 544;
					break;
				}
				default:
				{
					_topPlayerUI.x  = SceneChangeManager.getGameMinWidhtOffsize();
					_chatUI.x		=	_topPlayerUI.x;
					_rightTopIndexUI.x = - (SceneChangeManager.getGameMinWidhtOffsize()-200)
						_nextActivityUI.x	=	_rightTopIndexUI.x
					break;
				}
					
			}
			
			//居中容器
			middlePlace.x =  _topPlayerUI.x ;
			middlePlace.y = ( SceneChangeManager.getGameHeight()- 650 ) * .5 + SceneChangeManager.getGameY();
			
			if(_oldSceneType == GameDefine.SceneType_EscortScene)
			{
				_bottomIndexUI.x	=	middlePlace.x	+ 280;
				_bottomIndexUI.y	=	_chatUI.y + 1;
			}
			else if(_oldSceneType == GameDefine.SceneType_ForbiddenAreaScene)
			{
				_bottomIndexUI.x	=	- (SceneChangeManager.getGameMinWidhtOffsize()-200)
			}
			else
			{
				_bottomIndexUI.x = - (_topPlayerUI.x - 200);
			}
			
			
			if(_factionBattleUI .created == true)
			{
				_factionBattleUI.resUI();
			}
			
			if(ActionEctypeResultUI.getInstance().created )
			{
				ActionEctypeResultLogic.getInstance().addPanle();
			}
			
			RightNoticeLogic.getInstance().changePoint();
			
			
			SceneFrameLogic.getInstance().onChangeSize();
		}
		/**
		 * 隐藏所有UI
		 */
		public function hideAll():void
		{
			StageInfo.sceneSpace.visible	=	false;
			StageInfo.uiSpace.visible		=	false;
		}
		/**
		 * 还原显示
		 */
		public function showAll():void
		{
			StageInfo.sceneSpace.visible	=	true;
			StageInfo.uiSpace.visible		=	true;
		}
		
		//进入场景
		public function intoScene( sceneType : int = -1 ):void
		{
			ScenePlayerLogic._playerMax =30;
			SceneFrameLogic.getInstance().show = true;
			
			var scene : int = GameData.SceneType
			
			StageInfo.sceneSpace.visible	=	true;
			
			TopPlayerLogic.getInstance().fastTrackVisible	=	true;
			
			hideUI(ActivitesGroupLogic.getInstance().ui);
			
			hideUI(ForbiddenAreaUI.getInstance());
			
			hideUI(EscortSceneUI.getInstance());
			
			showUI(_chatUI, 	downPlace);
			
			RightNoticeLogic.getInstance().setVisible( false );
			if(sceneType	== GameDefine.SceneType_Attack)
			{//战斗场景
				SitCrossleggedLogic.getInstance().show = false;
				
				hideUI(SnowBossBloodUI.getInstance());
				hideUI(SnowBossExitUI.getInstance());
				hideUI(SnowBossRankUI.getInstance());
				hideUI(_bottomIndexUI);
				hideUI(_taskTrackingUI);
				hideUI(_rightTopIndexUI);
				hideUI(_topPlayerUI);
				hideUI(ForbiddenAreaUI.getInstance());
				hideUI(_bossrank);
				hideUI(FactionBossUI.getInstance());
				hideUI(FactionBossRankUI.getInstance());
				hideUI(FactionBossExitUI.getInstance());
				hideUI(PkListUI.getInstance());
				hideUI(TourUI.getInstance());
				hideUI(_factionBattleUI);
				hideUI(_nextActivityUI);
				_campBattleUI.visible=false;
				
				_oldSceneType	=	sceneType;
				
				onStageReSize();
				
				//强行设置焦点为聊天框
				StageInfo.stage.focus = _chatUI._inputTxt;
				return;
			}
			
			showUI(_bottomIndexUI,		downPlace);
			showUI(_taskTrackingUI,		topPlace);
			showUI(_topPlayerUI,		topPlace);
			showUI(_nextActivityUI, 	topPlace);
			showUI(_rightTopIndexUI,	topPlace);
			SitCrossleggedLogic.getInstance().show = true;
			
			ActivitesGroupLogic.getInstance().ui.visible	=	true;
			
			if( scene == GameDefine.SceneType_Normal )
			{
				showUI(ActivitesGroupLogic.getInstance().ui,RightTopIndexUI.getInstance());
				hideUI(_bossrank);
				hideUI(WorldBossExitUI.getInstance());
				hideUI(WorldBossBloodUI.getInstance());
				
				hideUI(FactionBossRankUI.getInstance());
				hideUI(FactionBossExitUI.getInstance());
				hideUI(FactionBossUI.getInstance());
				
				hideUI(SnowBossBloodUI.getInstance());
				hideUI(SnowBossExitUI.getInstance());
				hideUI(SnowBossRankUI.getInstance());
				
				RightNoticeLogic.getInstance().setVisible( true );
				
				
				//				if(EliteFBListLogic.getInstance().WindowIsOk==true)
				//				{
				//					EliteFBUI.getInstance().showOrHide();
				//				}
			}else if(scene == GameDefine.SceneType_WorldBossScene)
			{//世界boos
				showUI(_bossrank,downPlace);
				showUI(WorldBossExitUI.getInstance(),topPlace);
				showUI(WorldBossBloodUI.getInstance(),_uiPlace);
				hideUI(_rightTopIndexUI);
				hideUI(_nextActivityUI);
				hideUI(_taskTrackingUI);
			}
			else if(scene == GameDefine.SceneType_SnowBallFightScene)
			{//雪人boos
				showUI(SnowBossRankUI.getInstance(),downPlace);
				showUI(SnowBossExitUI.getInstance(),topPlace);
				showUI(SnowBossBloodUI.getInstance(),_uiPlace);
				TopPlayerLogic.getInstance().fastTrackVisible	=	false;
				hideUI(_rightTopIndexUI);
				hideUI(_nextActivityUI);
				hideUI(_taskTrackingUI);
			}
			else if(scene == GameDefine.SceneType_FactionBossScene)
			{//帮派boos
				showUI(FactionBossRankUI.getInstance(),downPlace);
				showUI(FactionBossExitUI.getInstance(),topPlace);
				showUI(FactionBossUI.getInstance(),_uiPlace);
				hideUI(_rightTopIndexUI);
				hideUI(_nextActivityUI);
				hideUI(_taskTrackingUI);
			}
			else if(scene == GameDefine.SceneType_ForbiddenAreaScene)
			{//禁地
				showUI(ForbiddenAreaUI.getInstance(),topPlace);
				hideUI(_topPlayerUI);
				hideUI(_nextActivityUI);
				hideUI(_taskTrackingUI);
			}
			else if(scene == GameDefine.SceneType_FactionScene)
			{
				ScenePlayerLogic._playerMax =50;
				if(SceneManager.getInstance().norSceneView.scenePlayerLogic.playerVisible == false)
				{
					SceneManager.getInstance().norSceneView.scenePlayerLogic.changePlayerVisible( true );
				}
				//帮会地图
				hideUI(_topPlayerUI);
				hideUI(_taskTrackingUI);
				hideUI(_rightTopIndexUI);
				hideUI(_nextActivityUI);
				_factionBattleUI.show();
			}
			_oldSceneType = scene;
			
			switch( sceneType )
			{
				case GameDefine.SceneType_WorldBossScene:
				case GameDefine.SceneType_SnowBallFightScene:
				case GameDefine.SceneType_FactionBossScene:
				case GameDefine.SceneType_Clone://天梯
				case GameDefine.SceneType_EscortScene://押镖
				case GameDefine.SceneType_Tour://游历
				case GameDefine.SceneType_Map://世界地图
				case GameDefine.SceneType_StrideMain:
				{//全屏ui
					UIManage.getInstance().hideOpenedUI();
					RightNoticeLogic.getInstance().setVisible( false );
					showFullScreenUI( sceneType );
				}
					
					_oldSceneType = sceneType;
			}
			
			onStageReSize();
			
			//强行设置焦点为聊天框
			StageInfo.stage.focus = _chatUI._inputTxt;
		}
		public function set autoFoundWay(val:Boolean):void
		{
			_autoFoundWay	=	val;
			
			if( !StoryMoveUI.IsStory )
			{
				_autoFoundWayIcon.visible	=	_autoFoundWay;
				if(_autoFoundWay)
				{
					_autoFoundWayIcon.gotoAndPlay(1);
				}else
				{
					_autoFoundWayIcon.stop();
				}
			}
		}
		
		public function get autoFoundWay():Boolean
		{
			return _autoFoundWay;
		}
		
		private function showFullScreenUI( type : int ):void
		{
			SceneFrameLogic.getInstance().show = false;
			ActivitesGroupLogic.getInstance().ui.visible	=	false;
			SitCrossleggedLogic.getInstance().show			=	false;
			TopPlayerLogic.getInstance().fastTrackVisible	=	true;
			
			hideUI(_taskTrackingUI);
			hideUI(_rightTopIndexUI);
			hideUI(_topPlayerUI);
			if(type != GameDefine.SceneType_EscortScene){
				hideUI(_bottomIndexUI);
			}
			hideUI(_nextActivityUI);
			
			StageInfo.sceneSpace.visible	=	false;
			
			if(type == GameDefine.SceneType_Clone)
			{//排名竞技场	
				showUI(PkListUI.getInstance(),	middlePlace);
			}
			else if(type == GameDefine.SceneType_EscortScene)
			{//押镖
				showUI(EscortSceneUI.getInstance(),	middlePlace);
			}
			else if(type	==	GameDefine.SceneType_Map)
			{//世界地图
				showUI(WorldMapLogic.getInstance().ui,	middlePlace);
				hideUI(ChatUI.getInstance());
				WorldMapLogic.getInstance().ui.x  = 0;
			}
			else if (type==GameDefine.SceneType_Tour)
			{//江湖游历
				showUI(_topPlayerUI, topPlace );
				showUI(TourLogic.getInstance()._ui,	middlePlace);
				hideUI(ChatUI.getInstance());
				UIManage.getInstance().hideOpenedUI();
				
				
				TopPlayerLogic.getInstance().fastTrackVisible	=	false;
				_topPlayerUI.y = 10;
			}
			else if(type	==	GameDefine.SceneType_StrideMain)
			{
				showUI(StrideMainLogic.getInstance().ui,	middlePlace);
				hideUI(ChatUI.getInstance());
			}
		}
		
		public function showUI(ui : *, parent : DisplayObjectContainer):void
		{
			ui.mouseChildren	=	ui.mouseEnabled	=	ui.visible	=	true;
			parent.addChild(ui);
			
			if(ui is BaseUI)
			{
				ui.x	=	ui.initPosX;
				ui.y	=	ui.initPosY;
				
				//				if(ui == PkListUI.getInstance()){
				//					PkListUI.getInstance().show();
				//				} else {
				( ui as BaseUI ).beforeShow();
				//				}
			}
		}
		
		public function hideUI(ui : Sprite):void
		{
			if(ui.parent != null)
			{
				ui.mouseChildren	=	ui.mouseEnabled	=	ui.visible	=	false;
				ui.parent.removeChild(ui);
				
				if(ui is BaseUI)
				{
					( ui as BaseUI ).afterHide();
				}
			}
		}
	}
}